package com.ktjy.web.servlet;

import com.alibaba.fastjson.JSONArray;
import com.ktjy.web.entity.AjaxResult;
import com.ktjy.web.entity.GoodsItem;
import com.ktjy.web.entity.Page;
import com.ktjy.web.entity.User;
import com.ktjy.web.service.GoodsService;
import com.ktjy.web.service.impl.GoodsServiceImpl;
import com.ktjy.web.util.BaseSevlet;
import my.framework.anno.RequestMapping;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

//@WebServlet(name = "GoodsListServlet", value = "/api/GoodsListServlet")
public class GoodsListServlet extends BaseSevlet {

    GoodsService goodsService=new GoodsServiceImpl();

    @RequestMapping("/GoodsPage")
    public Object GoodsPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String goodsName=request.getParameter("goods_name");
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));
        int pageNo=(Integer.parseInt(request.getParameter("pageNo"))-1)*pageSize;

        int count=goodsService.selGoodsCount();
        List<GoodsItem> list=goodsService.selPageByName(goodsName,pageNo,pageSize);

        Page<GoodsItem> page=new Page();
        page.setPageSize(pageSize);
        page.setTotalCount(count);
        page.setList(list);

        return AjaxResult.success("查询成功",page);
    }

    @RequestMapping("/delGoods")
    public Object DelGoods(HttpServletRequest request, HttpServletResponse response) throws IOException{
        int id= Integer.parseInt(request.getParameter("id"));

        int count=goodsService.delGoods(id);

        if (count>0){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.success("删除失败");
    }

    @RequestMapping("/selGoodsDetail")
    public Object GoodsById(HttpServletRequest request, HttpServletResponse response) throws IOException{
        int id=Integer.parseInt(request.getParameter("id"));

        GoodsItem goodsItem=goodsService.selGoodsById(id);

        return AjaxResult.success("用户详情查询",goodsItem);
    }

    @RequestMapping("/updateGoods")
    public Object UpdateGoods(HttpServletRequest request, HttpServletResponse response) throws IOException{
        int id=Integer.parseInt(request.getParameter("id"));
        String goods_name=request.getParameter("goods_name");
        int category_id=Integer.parseInt(request.getParameter("category_id"));
        int brand_id=Integer.parseInt(request.getParameter("brand_id"));
        String goods_img=request.getParameter("goods_img");
        String goods_desc=request.getParameter("goods_desc");
        double goods_price= Double.parseDouble(request.getParameter("goods_price"));

        GoodsItem goodsItem=new GoodsItem();
        goodsItem.setId(id);
        goodsItem.setGoodsName(goods_name);
        goodsItem.setCategoryId(category_id);
        goodsItem.setBrandId(brand_id);
        goodsItem.setGoodsImg(goods_img);
        goodsItem.setGoodsDesc(goods_desc);
        goodsItem.setGoodsPrice(BigDecimal.valueOf(goods_price));



        int count=goodsService.updateGoods(goodsItem);

        if (count>0){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.success("修改失败");
    }
}
